import QtQuick 2.0

Rectangle
{
    id: m_imageBtn
    width: 80
    height: 80
    property bool m_bCheck: false
    property string m_strBtnName: "Button"
    property string m_strBtnPicPath: "./../../Config/Skins/"+setting.m_skin+"/Icons/"
    property string m_strBtnId: ""

    property string m_strNormalImageSource:m_strBtnId===""?"":m_strBtnPicPath+m_strBtnId+"_Normal.png"
    property string m_strHoverImageSource:m_strBtnId===""?"": m_strBtnPicPath+m_strBtnId+"_Hover.png"
    property string m_strPressImageSource:m_strBtnId===""?"":m_strBtnPicPath+m_strBtnId+"_Press.png"
    property string m_strCheckImageSource:m_strBtnId===""?"":m_strBtnPicPath+m_strBtnId+"_Check.png"


    property bool m_btnTextVisible: true
    property string m_btnTextValue:"";

    property bool m_btnImageVisible:false;
    property color m_pressedbkcolor: g_Colors.m_cButtonPress
    property color m_normalbkcolor: g_Colors.m_cButtonNormal
    property color m_hoverbkcolor: g_Colors.m_cButtonHover
    property color m_checkbkcolor: g_Colors.m_cButtonCheck
    property color m_btnText: g_Colors.m_cButtonTextNormal

    signal signalLeftClick();
    signal signalRightClick();
    signal signalMouseEnter();
    signal signalMouseExit();

    color: m_normalbkcolor

    onM_bCheckChanged: {
        if ( m_bCheck ){
            m_image.source = m_imageBtn.m_strCheckImageSource
            m_imageBtn.color = m_pressedbkcolor
        }
        else{
            m_image.source = m_imageBtn.m_strNormalImageSource
            m_imageBtn.color = m_normalbkcolor
        }
    }

    Row
    {
        height: parent.height
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter

        Image {
            id: m_image
            smooth: true
            visible: m_btnImageVisible
            source: m_imageBtn.m_strNormalImageSource
            anchors.verticalCenter:  parent.verticalCenter
        }
        Item {
            visible: m_btnTextVisible
            width: m_btnTextValue.length * 15
            height: parent.height
            LQBaseText {
                id: m_text
                text: m_btnTextValue
                color: m_btnText
                font.bold: true
                font.pixelSize: m_imageBtn.height * 0.4
                anchors.verticalCenter: parent.verticalCenter
                //不显示图片时，按钮文字居中显示
                x: m_btnImageVisible?0:(parent.width - width )/2
            }
        }
    }

    MouseArea
    {
        anchors.fill: parent
        hoverEnabled: true
        acceptedButtons: Qt.AllButtons

        onEntered: {
            m_text.m_bScrolling = true
            m_image.source = m_imageBtn.m_strHoverImageSource
            m_imageBtn.color = m_hoverbkcolor
            m_imageBtn.signalMouseEnter()
        }

        onExited: {
            m_text.m_bScrolling = false
            if (m_bCheck )
            {
                m_image.source = m_imageBtn.m_strCheckImageSource
                m_imageBtn.color = m_pressedbkcolor
            }
            else{
                m_image.source = m_imageBtn.m_strNormalImageSource
                m_imageBtn.color = m_normalbkcolor
            }

            m_imageBtn.signalMouseExit();
        }

        onPressed: {
            m_image.source = m_imageBtn.m_strPressImageSource
            m_imageBtn.color = m_pressedbkcolor

            if ( mouse.button == Qt.LeftButton )
            {
                m_imageBtn.signalLeftClick();
            }
            else if ( mouse.button == Qt.RightButton )
            {
                m_imageBtn.signalRightClick();
            }
        }

        onReleased: {
            if ( m_bCheck )
            {
                m_image.source = m_imageBtn.m_strCheckImageSource
                m_imageBtn.color = m_checkbkcolor
            }
            else
            {
                if ( mouseX > 0 && ( mouseX < m_imageBtn.width) &&
                        (mouseY >= 0) && (mouseY < m_imageBtn.height))
                {
                    m_image.source = m_imageBtn.m_strHoverImageSource
                    m_imageBtn.color = m_hoverbkcolor
                }
                else {
                    m_image.source= m_imageBtn.m_strNormalImageSource
                    m_imageBtn.color = m_normalbkcolor
                }
            }
        }
    }
}
